// userPageC/pages/userPersonal/order/orderDetail/orderDetail.js
const app = getApp()
const WXAPI = require("../../../../../api/api")
const formatTime = require('../../../../../utils/formatTime.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '订单详情', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: ' ', // 导航栏背景颜色
      isloading: true,
      isColor: false,
    },
    orderId: '',
    orderData: {},
    countdown: '',
    timer: Object,
    second: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderId
    // var orderId = '1410445133110853633'
    this.setData({
      orderId: orderId
    })
    this.getDetail()
  },
  //订单详情
  getDetail(){
    var data = {
      id: this.data.orderId,
    }
    WXAPI.getOrderDetail( data, (res) =>{
      const data = res.result
      if(res.code === 200){
        console.log('订单详情',res)
        this.setData({
          orderData: data,
          second: Math.floor(data.unPaidCountdown / 1000)
        })
        if(data.payStatus == 1){
          this.countTime()
        }
        
      }else{
        wx.showToast({
          title: res.message,
          duration: 1500
        })
      }
    })
  },
  countTime() {
    var that = this;
    var leftTime =  this.data.second; //时间差     
    console.log('订单详情',this.data.second) 
    var newSecond = leftTime --   
    var  m, s;
    if (leftTime >= 0) {
      m = Math.floor(leftTime/ 60 % 60);
      s = Math.floor(leftTime % 60);
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      that.setData({
        second: leftTime,
        countdown:  '请在' +m+'分'+s+'秒内完成订单' 
      })
      //递归每秒调用countTime方法，显示动态时间效果
      console.log(that.data.countdown)
      setTimeout(that.countTime, 1000);
    } else {
      console.log('已截止')
    }
    
  },
  //取消订单
  cancelOrder(e){
    var that = this
    var id = e.currentTarget.dataset.id
    wx.showModal({
      title: '是否取消订单',
      cancelText: '取消',
      confirmColor:'#FF5800',
      confirmText: '确定',
      success (res) {
        if (res.confirm) {
          var data = {
            id: id
          }
          WXAPI.cancelOrder( data, (res) =>{
            const data = res.result
            if(res.code === 200){
              console.log('取消订单',res)
              wx.showToast({
                title: '取消成功',
                duration: 1500
              })
              that.getDetail()
              that.refresh()
            } 
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },

  //支付订单
  payOrder(e){
    var id = e.currentTarget.dataset.id
    var data = {
      id: id
    }
    WXAPI.payOrder( data, (res) =>{
      const data = res.result
      if(res.code === 200){
        console.log('支付订单',res)
        this.wxpay(data)
      } 
    })
  },
  //拉起微信支付
  wxpay(data){
    var that = this
    console.log('data',data)
    wx.requestPayment({
      'timeStamp': data.timeStamp,
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType ,//MD5
      'paySign': data.paySign,
      'success': function (res) { 
          console.log('支付成功',res)
          console.log(res)
          if(res.errMsg == 'requestPayment:ok'){
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 1200
            })
           
            setTimeout(() => {
              that.getDetail()
              that.refresh()
            }, 1000);
          
          }
        },
        'fail': function (res) { 
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
          console.log('支付失败或取消支付',res)
        },
        'complete': function (res) { 
          console.log('支付完成',res)
          // wx.navigateBack({})
        }
    })
  },
  //刷新上一级页面列表
  refresh(){
    let pages = getCurrentPages(); 
    console.log("pages['0'].route",pages[pages.length - 2].route)
    let prevpage = pages[pages.length - 2]
    prevpage.setData({
      pageNum: 1,
      orderList: [],
    })
    prevpage.getList()
  },  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      second: 0
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})